﻿using coreapi.Models.TempModels;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Models;
using SqlSugar;
using System.Collections.Generic;
using System.Drawing.Printing;
using coreapi.Helpers;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace coreapi.Controllers
{
    [EnableCors("any")]
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "v1")]
    public class HomeController : ControllerBase
    {
        private readonly ISqlSugarClient db;
        public HomeController(ISqlSugarClient _db)
        {
            this.db = _db;
        }


        /// <summary>
        /// 获取员工信息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pagenumber"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public object Get(string? name, int pagenumber, int pageSize)
        {

            int totalCount = 0;
            // var list = db.Queryable<EmployeeUser>();
            var newlist = db.Queryable<EmployeeUser, EmployeeSalary, EmployeeRole>((u, s, r) => u.SalaryID == s.SalaryID && u.RoleID == r.RoleID)
                            .Select((u, s, r) => new UserInfo
                            {
                                UID = u.UID,
                                UName = u.UName,
                                Password = u.UPassword,
                                UMailbox = u.UMailbox,
                                UDate = u.UDate,
                                ActualSalary = s.ActualSalary,
                                RoleName = r.RoleName,
                                RoleID = r.RoleID,
                                SalaryID=s.SalaryID,
                            });

            if (!string.IsNullOrEmpty(name)) {
                //模糊查询
                newlist.Where(u => u.UName.Contains(name));
            }
            //单表分页
            var data = new
            {
                data = newlist.ToPageList(pagenumber, pageSize, ref totalCount),
                totalCount = totalCount
            };
            return data;
        }
        /// <summary>
        /// 添加员工数据
        /// </summary>
        /// <param name="value"></param>
        // POST api/<HomeController>
        [HttpPost]
        public IActionResult Post([FromBody] EmployeeUser emp)
        {
            emp.UDate = DateTime.Now;
            emp.UPassword=MD5Help.MD5Sign(emp.UPassword);
            int i = db.Insertable(emp).ExecuteCommand();
            
            return Ok(i > 0 ? true : false);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        [HttpPut]
        public IActionResult Put([FromBody] EmployeeUser emp)
        {
           // emp.SalaryID = 1;
            var result = db.Updateable(emp).ExecuteCommand();//实体有多少列更新多少列
            return Ok(result > 0 ? true : false);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        [HttpDelete("De")]
        public IActionResult De(int uid)
        {
            int result = db.Deleteable<EmployeeUser>().In(uid).ExecuteCommand();
            return Ok(result > 0 ? true : false);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        [HttpDelete]
        public IActionResult Delete(List<int> arr)
        {
            int result = db.Deleteable<EmployeeUser>().In(arr).ExecuteCommand();
            return Ok(result > 0 ? true : false);
        }

 
    }
}
